﻿using Common;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.InfantBaby
{
    public partial class InfantBabyPrint : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        DateTime Start;
        DateTime End;
        Tb_User LoginUser;
        ElectronicSignature es;
        #endregion

        #region 构造函数
        public InfantBabyPrint(Tb_User loginUser, PatientInfo patientInfo, DateTime StartDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            if ((PatientInfo.PATIENT_ID.Contains("$1$1")) || (PatientInfo.PATIENT_ID.Contains("$2$2")))//临时处理方式，需检查基本信息表
            {
                PatientInfo.PATIENT_ID = PatientInfo.PATIENT_ID.Substring(0, PatientInfo.PATIENT_ID.Length - 2);
            }
            Start = StartDate;
            End = EndDate;
            es = new ElectronicSignature(this.LoginUser);
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\婴儿护理记录单.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表取数事件
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(PatientInfo.PATIENT_ID.Trim(), PatientInfo.VISIT_ID, (int)FormType.BybyAndBaby, Start, End);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    Byays ps = XmlUtil.XMLToObject<Byays>(record.NursingFormData, Encoding.UTF8);
                    string image1 = "";
                    string RYHS = ps.HuShiQianMing;
                    if (!string.IsNullOrEmpty(RYHS))
                    {
                        image1 = es.GetElectricSignBase64(record.AreaId, ps.HuShiQianMing);
                    }
                    DataRow row = dt.NewRow();
                    row["NURSE_CELL_NAME"] = PatientInfo.NURSE_CELL_NAME;//NURSE_CELL_NAME,
                    row["BedNo"] = PatientInfo.BED_NO;//BED_NO,
                    row["PATIENTNAME"] = record.PatientName;
                    row["SEX"] = PatientInfo.SEX;//SEX
                    row["AGE"] = PatientInfo.AGE_DESC;//AGE
                    row["PatientId"] = PatientInfo.PATIENT_ID;//PATIENTID
                    row["DEPARTMENT_NAME"] = PatientInfo.DEPARTMENT_NAME;//DEPARTMENT_NAME
                    row["riqi"] = !string.IsNullOrEmpty(ps.RiQi) ? Convert.ToDateTime(ps.RiQi).ToString("yyyy-MM-dd") : ((DateTime)record.AssessmentDate).ToString("yyyy-MM-dd");
                    row["shijian"] = DateTimeHelper.HHmmssToHHmm(ps.ShiJian);
                    row["tiwen"] = ps.YiBanQingKuangTiWen;
                    row["fanying"] = ps.YiBanQingKuangFanYing;
                    row["fuse"] = ps.YiBanQingKuangFuSe;
                    row["chunmuru"] = ps.WeiYangChunMuRu;
                    row["peifangnai"] = ps.WeiYangPeiFangNai;
                    row["dabiancishu"] = ps.DaBianCiShu;
                    row["dabianyanse"] = ps.DaBianYanSe;
                    row["xiaobiancishu"] = ps.XiaoBianCiShu;
                    row["xingzhi"] = ps.OuTuXingZhi;
                    row["liang"] = ps.OuTuLiang;
                    row["qidai"] = ps.QiDai;
                    row["kusheng"] = ps.KuSheng;
                    row["yingerqingkuangjichuli"] = ps.YingErQingKuangJiChuLi;
                    row["hushiqianming"] = ps.HuShiQianMing;
                    row["ElectricSignBase64"] = image1;
                    dt.Rows.Add(row);
                }

                dt.DefaultView.Sort = "riqi ASC,shijian ASC";
                DataTable data = dt.DefaultView.ToTable();
                Utility.FillRecordToReport(Report, data);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("BedNo", typeof(string));
            dt.Columns.Add("PATIENTNAME", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("riqi", typeof(string));
            dt.Columns.Add("shijian", typeof(string));
            dt.Columns.Add("tiwen", typeof(string));
            dt.Columns.Add("fanying", typeof(string));
            dt.Columns.Add("fuse", typeof(string));
            dt.Columns.Add("chunmuru", typeof(string));
            dt.Columns.Add("peifangnai", typeof(string));
            dt.Columns.Add("dabiancishu", typeof(string));
            dt.Columns.Add("dabianyanse", typeof(string));
            dt.Columns.Add("xiaobiancishu", typeof(string));
            dt.Columns.Add("xingzhi", typeof(string));
            dt.Columns.Add("liang", typeof(string));
            dt.Columns.Add("qidai", typeof(string));
            dt.Columns.Add("kusheng", typeof(string));
            dt.Columns.Add("yingerqingkuangjichuli", typeof(string));
            dt.Columns.Add("hushiqianming", typeof(string));
            dt.Columns.Add("ElectricSignBase64", typeof(string));//电签
        }
        #endregion

        #region 窗体加载启动锐浪报表
        private void InfantBabyPrint_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
    }
}
